/*
 * Isman Usoh<isman.usoh@gmail.com>
 * www.facebook.com/isman.usoh
 */
package com.isman.postcodeinfo;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.isman.postcodeinfo.database.PostCode;

public class PostMapActivity extends MapActivity {

	
	private MapView mapView;
	private PostCode mPostCode;
	private MyItemizedOverlay userPicOverlay;
	private MyItemizedOverlay postOfficeOverlay;
	private OverlayItem userItem;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_postmap);
		
		
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setClickable(true);
		
		ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
		zoomControls.setOnZoomInClickListener(new OnClickListener() {
		        public void onClick(View v) {
		            mapView.getController().zoomIn();
		        }
		});
		zoomControls.setOnZoomOutClickListener(new OnClickListener() {
		        public void onClick(View v) {
		            mapView.getController().zoomOut();
		        }
		});
		
		Log.i("Isman", "Post Map");
		
		
		mPostCode = (PostCode) getIntent().getSerializableExtra(DetailActivity.DATA);
		
		getCurrentLocation();
		getPostOfficeLocation();
	}

	private void getCurrentLocation() {
		LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		LocationListener ll = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(final Location location) {
				if(location != null){
					if(userPicOverlay != null){
						mapView.getOverlays().remove(userPicOverlay);
					}
					userPicOverlay = new MyItemizedOverlay(getResources().getDrawable(R.drawable.ic_maps_indicator_current_position));
		        	GeoPoint geoPoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
					userItem = new OverlayItem(geoPoint, "คุณอยู่ที่นี้", null);
		            userPicOverlay.addOverlay(userItem);
		            mapView.getOverlays().add(userPicOverlay);
				}
			}
		};
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
	}
	
	private void getPostOfficeLocation(){
		
        postOfficeOverlay = new MyItemizedOverlay(this.getResources().getDrawable(R.drawable.postoffice));
        GeoPoint currentPoint = new GeoPoint((int)(mPostCode.getLlat() * 1E6), (int)(mPostCode.getLlong() * 1E6));
		OverlayItem item = new OverlayItem(currentPoint, "ไปรษณี "+mPostCode.getName(), null);
		postOfficeOverlay.addOverlay(item);
        mapView.getOverlays().add(postOfficeOverlay);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	    private ArrayList<OverlayItem> myOverlays ;

	    public MyItemizedOverlay(Drawable defaultMarker) {
	        super(boundCenterBottom(defaultMarker));
	        myOverlays = new ArrayList<OverlayItem>();
	        populate();
	    }

	    public void addOverlay(OverlayItem overlay){
	        myOverlays.add(overlay);
	        populate();
	    }

	    @Override
	    protected OverlayItem createItem(int i) {
	        return myOverlays.get(i);
	    }

	    public void removeItem(int i){
	        myOverlays.remove(i);
	        populate();
	    }

	    @Override
	    public int size() {
	        return myOverlays.size();
	    }

	    public void addOverlayItem(OverlayItem overlayItem) {
	        myOverlays.add(overlayItem);
	        populate();
	    }
	    
	    public void move(int index){
	    	mapView.getController().animateTo(myOverlays.get(index).getPoint());
	    }

	    public void addOverlayItem(int lat, int lon, String title) {
	        try {
	            GeoPoint point = new GeoPoint(lat, lon);
	            OverlayItem overlayItem = new OverlayItem(point, title, null);
	            addOverlayItem(overlayItem);    
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	    
	    public int getCount(){
	    	return this.myOverlays.size();
	    }

	    @Override
	    protected boolean onTap(int index) {
	        AlertDialog.Builder builder = new AlertDialog.Builder(PostMapActivity.this);
	        builder.setTitle(mPostCode.getName()+" ("+mPostCode.getPostcodel()+")");
	        builder.setMessage(mPostCode.getAddress()+mPostCode.getDistrict()+mPostCode.getProvince())
	               .setPositiveButton("Close", new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   dialog.dismiss();
	                   }
	               });
	        // Create the AlertDialog object and return it
	        builder.create();
	        builder.show();
	        return super.onTap(index);
	    }
	}

}
